package com.java.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangning
 * @create 2021-10-14 8:49
 */
public class TestJsonToObject {
	public static void main(String[] args) throws IOException {
		HashMap<String, Object> map = new HashMap<>();
		ArrayList<Object> list = new ArrayList<>();
		HashMap<String, String> map1 = new HashMap<>();
		map1.put("aa", "11");
		map1.put("bb", "22");
		list.add(map1);
		map.put("a", list);
		ObjectMapper objectMapper = new ObjectMapper();
		String s = objectMapper.writeValueAsString(map);
		System.out.println("s = " + s);

		String ss = "{\"a\":[{\"aa\":\"11\",\"bb\":\"22\"}]}";
		Map obj = objectMapper.readValue(ss, Map.class);
		System.out.println("map" + obj.toString());

		Object parse = JSON.parse(ss);
		System.out.println(parse.toString());
		JSONObject jsonObject = JSON.parseObject(ss);
		System.out.println(jsonObject);

		Map map2 = JSON.parseObject(ss, Map.class);
		System.out.println("map2 = " + map2);

		String str = "{\"item1\":\"000000\",\"company_id\":\"010026606\",\"data\":[{\"contract_no\":\"15.000\",\"end_date\":\"201403\",\"no\":\"1\",\"field1\":\"201403\",\"balance\":\"400\",\"late_fee\":\"0\",\"pay_amount\":\"6000\",\"begin_date\":\"201403\",\"customer_name\":\"武圣西里 5 楼 3 门 601\"}],\"ceb_track\":\"20200902217114715579\",\"bill_key\":\"050405543\",\"total_num\":\"1\",\"inst_id\":\"100000000000217\",\"item3\":\"60.00\"}";
		Map map3 = JSON.parseObject(str, Map.class);
		System.out.println(map3);
		Map obj1 = objectMapper.readValue(str, Map.class);
		System.out.println("map" + obj1.toString());

		Gson gson = new Gson();
		Map map4 = gson.fromJson(str, Map.class);
		System.out.println(map4);

		String str1 = "{\"title\":\"使用情况\",\"options\":[{\"name\":\"好\",\"value\":1},{\"name\":\"不好\",\"value\":2},{\"name\":\"还行\",\"value\":3}]}";
		Map obj2 = objectMapper.readValue(str1, Map.class);
		System.out.println("map" + obj2.toString());

	}
}
